/*
 * UART.cpp
 *
 *      Author: Christian
 *      Letztes Update: 11.01.14 Christian | fähler rausmachen
 *
 */

#include "UART.h"

uint8_t send_buffer[SEND_BUFFER_SIZE];

volatile uint8_t receive_buffer[RECEIVE_BUFFER_SIZE];
volatile uint8_t received_number = 0;

void UARTInit() {
	//BAUD-Rate einstellen 9600
	UBRRH = (unsigned char) (103 >> 8);
	UBRRL = (unsigned char) 103;

	//Sender einstellen
	UCSRB = (1 << TXEN) | (1 << RXEN) | (1 << RXCIE);

	//Frame einstellen: 8data, 2stop bit
	UCSRC = (1 << URSEL) | (1 << USBS) | (1 << UCSZ0) | (1 << UCSZ1);
}

void UART_send(uint8_t bytes) {
	// while( !( UCSRA & ( 1 << UDRE ) ) );
	for (uint8_t i = 0; i < bytes; i++) {
		UDR = send_buffer[i];
		while (bit_is_clear(UCSRA, UDRE))
			;
	}
}

void sendDebugMessage() {
	UART_send( ANZAHL_DEBUG_WERTE);
}

// Empfang von Anweisungen
ISR( USART_RXC_vect ) {

	receive_buffer[received_number] = UDR;
	received_number++;
}

void reset_receive_buffer() {

	for (uint8_t i = 0; i < received_number; i++) {
		receive_buffer[i] = 0;
	}
	received_number = 0;

}
void reset_send_buffer() {
	for (uint8_t i = 0; i < SEND_BUFFER_SIZE; i++) {
		send_buffer[i] = 0;
	}
}

